/**
 * 
 */
package com.orange.cepappmanager.client.rest;

/**
 * @author GQXG9096
 *
 */

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.orange.cepappmanager.CEPAppEnginCommon;
import com.orange.cepappmanager.CEPAppEnginCommon.CommandCEP;
import com.orange.cepappmanager.model.Phone;
import com.orange.cepappmanager.model.converter.PhoneConverter;
import com.orange.cepappmanager.tools.GCMUtilities;
import com.orange.cepappmanager.tools.ServerUtilities;
import static com.orange.cepappmanager.CEPAppEnginCommon.*;

@Path("/phone/")
public class PhoneService {

	/**
	 * Log
	 */
	private static final Logger log = Logger.getLogger(PhoneService.class
			.getName());
	HttpURLConnection conn = null;
	ServerUtilities.Method method;
	StringBuilder bodyBuilder = new StringBuilder();
	String body = bodyBuilder.toString();
	byte[] bytes = body.getBytes();

	/**
	 * Registration phone on the server
	 * 
	 * @param deviceId
	 * @return string
	 */
	@PUT
	@Produces({ "application/xml", "application/json" })
	@Path("/register/{id}/{name}/{status}")
	public PhoneConverter register(@PathParam("id") String id,
			@PathParam("name") String name, @PathParam("status") String status) {
		log.info("Ajout d'un device phone");
		Phone ph = new Phone();
		ph.setId(id);
		ph.setName(name);
		ph.setStatus(status);
		try {
			log.info("register OK : " + ph.toString());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			// em.close();

		}

		PhoneConverter converter = new PhoneConverter(ph);
		return converter;

	}

	/**
	 * @return phones
	 */
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/allphones/")
	public List<PhoneConverter> allPhones() {
		log.info("liste phones");
		List<PhoneConverter> phonesConverters = new ArrayList<PhoneConverter>();
		return phonesConverters;
	}

	
	/**
	 * test server GCM
	 * 
	 * @param deviceId
	 * @param cmd command control of cep(Start, Stp ...)
	 * @return
	 */
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/cepControle/{command}/")
	public PhoneConverter cepControle(@PathParam("command") CommandCEP cmd) {
		// dummy code
		Phone ph = new Phone();
		ph.setId(ID_NEXSUS);
		ph.setName("Nexus s");
		ph.setStatus("test gcm send command cep");
		PhoneConverter converter = new PhoneConverter(ph);
		try {
			GCMUtilities.sendMessageToPhone(ID_NEXSUS, cmd ,"hello client");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.print(e.getMessage() + " : send notification error");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return converter;
	}
	
	/**
	 *  allCepControle send message to all devices
	 * 
	 * @param cmd command control of cep(Start, Stp ...)
	 * @return
	 */
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/allCepControle/{command}/")
	public PhoneConverter allCepControle(@PathParam("command") CommandCEP cmd) {
		// dummy code
		Phone ph = new Phone();
		ph.setId("multicast GCM");
		ph.setName("Nexus s, galaxu s2");
		ph.setStatus("test gcm send command cep");
		PhoneConverter converter = new PhoneConverter(ph);
		try {
			GCMUtilities.sendMessageToAllPhone(cmd ," multicast test");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.print(e.getMessage() + " : send notification error");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return converter;
	}
	
	/*
	 * ==========================================================================
	 * = * GCM methode test * *
	 * ======================================================================
	 */
	
	/**
	 * test web service
	 * 
	 * @return
	 */
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/test/")
	public PhoneConverter getPhone() {
		// dummy code
		Phone ph = new Phone();
		ph.setId("100");
		ph.setName("Nexus");
		ph.setStatus("test web services");
		PhoneConverter converter = new PhoneConverter(ph);
		Map<String, Object> params = new HashMap<String, Object>();
		List<Phone> phones = new ArrayList<Phone>();
		phones.add(ph);
		params.put("data", ph);
		ServerUtilities.Method method = ServerUtilities.Method.POST;
		try {
			ServerUtilities.post(CEPAppEnginCommon.PATH_REGISTER_PHONE, params, method);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.print(e.getMessage() + " : " + params.toString());
		}
		return converter;
	}
	
	/**
	 * test server GCM
	 * 
	 * @param deviceId
	 * @return
	 */
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/sendnotification/")
	public PhoneConverter sendNotification() {
		// dummy code
		Phone ph = new Phone();
		ph.setId(ID_NEXSUS);
		ph.setName("galaxy s2");
		ph.setStatus("test gcm send msg");
		PhoneConverter converter = new PhoneConverter(ph);
		try {
			GCMUtilities.sendMessageToPhone(ID_NEXSUS,CommandCEP.message, "hello client");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.print(e.getMessage() + " : send notification error");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return converter;
	}
	
	
	/**Test server GCM
	 *  allCepControleTest send message to all devices
	 * 
	 * @param cmd command control of cep(Start, Stp ...)
	 * @return
	 */
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/allCepControleTest")
	public PhoneConverter allCepControle() {
		// dummy code
		Phone ph = new Phone();
		ph.setId("multicast GCM");
		ph.setName("Nexus s, galaxu s2");
		ph.setStatus("test gcm send command cep");
		PhoneConverter converter = new PhoneConverter(ph);
		try {
			GCMUtilities.sendMessageToAllPhone(CommandCEP.message ," multicast test");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.print(e.getMessage() + " : send notification error");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return converter;
	}
	
	
	/**
	 * test server GCM
	 * 
	 * @param deviceId
	 * @param cmd command control of cep(Start, Stp ...)
	 * @return
	 */
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/cepControleTest/")
	public PhoneConverter cepControleTest() {
		// dummy code
		Phone ph = new Phone();
		ph.setId(ID_NEXSUS);
		ph.setName("Nexus s");
		ph.setStatus("test gcm send command cep start");
		PhoneConverter converter = new PhoneConverter(ph);
		try {
			GCMUtilities.sendMessageToPhone(ID_NEXSUS,CommandCEP.startCEPEngine ," start CEP Engine");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.print(e.getMessage() + " : send notification error");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return converter;
	}
}
